<template>
	<view>
		<!-- u-p-30设置边距30，u-p-r-20设置右边距20 -->
		<view class="u-flex user-box  u-p-30" @click="gotoMyInfo">
			<view class="u-m-r-10">
				<u-avatar :src="avatar || '/static/pic/user.png'" size="140"></u-avatar>
			</view>
			<view class="u-flex-1">
				<view class="u-font-18 u-p-b-20">{{nickname}}</view>
			</view>
			<view class="u-m-l-10 u-p-10">
				<u-icon name="arrow-right" color="#969799" size="28"></u-icon>
			</view>
		</view>

		<view class="u-m-t-20" @click="gotoCart">
			<u-cell-group>
				<!-- 此处的icon图标：https://vkuviewdoc.fsq.pub/components/icon.html -->
				<u-cell-item icon="shopping-cart" title="我的收藏"></u-cell-item>
			</u-cell-group>
		</view>

		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item @click="gotoMyPublished" icon="plus-circle" title="我发布的"></u-cell-item>
				<u-cell-item @click="gotoMyPurchases" icon="rmb-circle" title="我买到的"></u-cell-item>
				<u-cell-item @click="gotoMySales" icon="bag" title="我卖出的"></u-cell-item>
				<u-cell-item @click="gotoMyReviews" icon="chat" title="我的评价"></u-cell-item>
			</u-cell-group>
		</view>

		<view class="u-m-t-20">
			<u-cell-group>
				<!-- 设置里面可以继续实现退出账号，修改密码等功能 -->
				<u-cell-item icon="setting" title="设置" @click="gotoSettings"></u-cell-item>
			</u-cell-group>
		</view>
	</view>
</template>

<script setup>
	import {
		onShow
	} from '@dcloudio/uni-app'
	import {
		ref
	} from 'vue'
	import {
		getUserInfoApi
	} from '../../api/user.js'
	const pic = ref('https://uviewui.com/common/logo.png')
	const show = ref(true)

	const nickname = ref('用户昵称')
	const avatar =ref('')
	// 获取用户信息
	const fetchUserInfo = async () => {
		try {
			const userId = uni.getStorageSync('userId')
			const res = await getUserInfoApi(userId)

			if (res && res.code == 200) {
				console.log(res.data)
				nickname.value = res.data.nickname
				avatar.value = res.data.avatar
			}
		} catch (error) {
			console.error(error)
		}
	}

	const gotoMyInfo = () => {
		uni.navigateTo({
			url: '/pages/MyInfo/MyInfo',
		})
	}
	// 跳转到购物车界面
	const gotoCart = () => {
		uni.switchTab({
			url: '/pages/cart/cart',
		})
	}
	// 跳转到我的发布界面
	const gotoMyPublished = () => {
		uni.navigateTo({
			url: '/pages/MyPublished/MyPublished',
		})
	}
	// 跳转到我卖出的界面
	const gotoMySales = () => {
		uni.navigateTo({
			url: '/pages/MySales/MySales',
		})
	}
	// 跳转到我买到的的界面
	const gotoMyPurchases = () => {
		uni.navigateTo({
			url: '/pages/MyPurchases/MyPurchases',
		})
	}
	// 跳转到我的评价的界面
	const gotoMyReviews = () => {
		uni.navigateTo({
			url: '/pages/commentList/commentList'
		})
	}
	// 跳转到设置界面
	const gotoSettings = () => {
		uni.navigateTo({
			url: '/pages/settings/settings',
		})
	}



	onShow(() => {
		fetchUserInfo()
	})
</script>

<style lang="scss">
	page {
		background-color: #ededed;
	}

	.camera {
		width: 54px;
		height: 44px;

		&:active {
			background-color: #ededed;
		}
	}

	.user-box {
		background-color: #fff;
	}
</style>